<?php
class Admin_AuthController extends Apps_Controller_Action_Admin {

	public function indexAction() {
	    $this->disallowBacktrack();
		$storage = new Zend_Auth_Storage_Session();
		$data = $storage->read();
		if(!$data){
			$this->_redirect('/vct/in');
		}
		$this->view->data = $data;
	}

	public function changePasswordAction() {
		$rq = $this->_request;
		$id = $rq->getParam('id');
        $form = new Admin_Form_UserChangePassword();
        $tbUser = new M_DbTable_User();
        $r = $tbUser->fetchRow(array('id = ?' => $id));

		if(!$r) {
            $this->_flashMessenger->addMessage(self::$msgNoRecordFound);
            return $this->goToActionIndex();
        }

        if($rq->isPost()) {
            if($form->isValid($rq->getParams())) {
            	$d = $form->getValues();
            	if($r['password'] != md5($d['password'])) {
            		$form->getElement('password')->addError('old password is incorect.');
            	} else {
	            	$newPassword = md5($d['newPassword']);
	            	$r = $tbUser->safelyUpdate(array('password' => $newPassword), array('id = ?' => $id));
	                $this->_flashMessenger->addMessage(
	                    $r
	                    ? self::$msgUpdateSuccess
	                    : self::$msgUpdateFailed);
	                return $this->goToActionIndex();
            	}
            }
        } else {
        }
        $this->view->form = $form;

	}

	public function logoutAction() {
		$this->disallowBacktrack();
		$storage = new Zend_Auth_Storage_Session();
		$storage->clear();
		$this->_redirect('/vct/in/');
	}
}